package middleware

import (
	"context"
	"dcim/pkg/generic-go-kit/core"
	"fmt"
	"github.com/go-kit/kit/endpoint"
)

func NewResponseEncoder() endpoint.Middleware {
	return func(next endpoint.Endpoint) endpoint.Endpoint {
		return func(ctx context.Context, request interface{}) (response interface{}, err error) {

			v, ok := ctx.Value(core.RequestIDKey).(string)
			if !ok {
				fmt.Println(core.RequestIDKey + " --> " + "empty request id")
				//panic(errors.New("empty request id"))
			} else {
				fmt.Println(core.RequestIDKey + " --> " + v)
			}

			r, err := next(ctx, request)
			if err != nil {
				return core.ErrResp("", err), nil
			}
			return core.SuccessResp("", r), nil
		}
	}
}
